//
//  SLNearHotAlertView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/30.
//

#import "SLNearHotAlertView.h"

@implementation SLNearHotAlertView
-(void)awakeFromNib{
    [super awakeFromNib];
    
    _bgView.layer.cornerRadius = 10;
    _bgView.layer.masksToBounds = YES;
    
    [_sureBtn setTitle:kLocalString(@"Confirm") forState:UIControlStateNormal];
    _sureBtn.btnStyle = 1;
    _topLbl.text = kLocalString(@"Set hot area");
    
    // 设置 slider 的最小值和最大值
    _slider.value = 100;
    _slider.minimumValue = 100;
    _slider.maximumValue = 250;
    // 添加滑动手势监听
    [_slider addTarget:self action:@selector(sliderTouchBegan:) forControlEvents:UIControlEventTouchDown];
    [_slider addTarget:self action:@selector(sliderTouchEnded:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
    _slider.tintColor = kHexColor(@"#0C0C0C");
    _slider.maximumTrackTintColor = kHexColor(@"#0C0C0C");
    _slider.minimumTrackTintColor = kHexColor(@"#0C0C0C");
    _slider.layer.cornerRadius = 2.5;
    _slider.layer.masksToBounds = YES;
    _sliderTitleLbl.text = [NSString stringWithFormat:@"100m(%@)",kLocalString(@"Default")];
    

}

- (IBAction)sureAction:(id)sender {
    if (self.btnBlock) {
        self.btnBlock(1,@(_value));
    }
}

- (void)sliderTouchBegan:(UISlider *)sender {
    self.isSliding = YES;
}

- (void)sliderTouchEnded:(UISlider *)sender {
    self.isSliding = NO;
    // 计算最接近的允许值
    [self updateSliderValue:sender.value];
}

- (void)updateSliderValue:(int)value {
    // 将值限制在 100、150、200 和 250 之间
    
    if (value < 125) {
        value = 100;
    } else if (value < 175) {
        value = 150;
    } else if (value < 225) {
        value = 200;
    } else {
        value = 250;
    }
    _value = value;
    // 更新 slider 的值和标签显示
    self.slider.value = value;
    NSString *desc = kLocalString(@"Within a range of <1> meters, the app will automatically search and connect to the padlock.");
    desc = [desc stringByReplacingOccurrencesOfString:@"<1>" withString:kSTRING(@(value))];
    _tipLbl.text = desc;
    
    if (value == 100) {
        self.sliderTitleLbl.text = [NSString stringWithFormat:@"%dm(%@)", value,kLocalString(@"Default")];
    }else{
        self.sliderTitleLbl.text = [NSString stringWithFormat:@"%dm", value];
    }
    if (self.updateRangeBlock) {
        self.updateRangeBlock(value);
    }
}


@end
